﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation.ConfigSetting
{
    public partial class InfraredSetForm : Form
    {
        public InfraredSetForm()
        {
            InitializeComponent();
        }

        private void InfraredSetForm_Load(object sender, EventArgs e)
        {
            int way = int.Parse(ConfigurationManager.AppSettings["InfraredWay"]);
            this.chkInfraredWayList.SelectedIndex = way;
            this.chkIsTurnOver.Checked = int.Parse(ConfigurationManager.AppSettings["IsTurnover"]) == 0 ? true : false;

            this.txtLeftIp.Text = ConfigurationManager.AppSettings["IpOfLeftInfraredIO"].ToString();
            this.txtLeftFrontPoint.Text = ConfigurationManager.AppSettings["LeftFInfrared"].ToString();
            this.txtLeftBackPoint.Text = ConfigurationManager.AppSettings["LeftBInfrared"].ToString();
            this.txtRightIP.Text = ConfigurationManager.AppSettings["IpOfRightInfraredIO"].ToString();
            this.txtRightFrontPoint.Text = ConfigurationManager.AppSettings["RightFInfrared"].ToString();
            this.txtRightBackPoint.Text = ConfigurationManager.AppSettings["RightBInfrared"].ToString();

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["InfraredWay"].Value = this.chkInfraredWayList.SelectedIndex.ToString();
            cfa.AppSettings.Settings["IsTurnover"].Value = chkIsTurnOver.Checked == true ? "0" : "1";

            cfa.AppSettings.Settings["IpOfLeftInfraredIO"].Value = this.txtLeftIp.Text.Trim();
            cfa.AppSettings.Settings["LeftFInfrared"].Value = this.txtLeftFrontPoint.Text.Trim();
            cfa.AppSettings.Settings["LeftBInfrared"].Value = this.txtLeftBackPoint.Text.Trim();
            cfa.AppSettings.Settings["IpOfRightInfraredIO"].Value = this.txtRightIP.Text.Trim();
            cfa.AppSettings.Settings["RightFInfrared"].Value = this.txtRightFrontPoint.Text.Trim();
            cfa.AppSettings.Settings["RightBInfrared"].Value = this.txtRightBackPoint.Text.Trim();

            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
